package edu.gatech.oad.mule;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/**
 * This class represents the panel which contains
 * all of the store's GUI components
 *
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 * @version %I%, %G%
 */
public class StorePanel extends JPanel{
	
	private JButton buy;
	private JButton sell;
	private JComboBox buySelector;
	private JComboBox sellSelector;
	private JComboBox resourceChooser;
	private JLabel resource;
	private JLabel market;
	private JLabel marketValue;
	
	private int amount;
	private int cost;
	private Player p;
	private String[] resourceList = {"Crystite", "Energy", "Food", "Smithore"};
	private String selectedResource;
	private Wallet w;	
	
	private int foodValue = 25;
	private int energyValue = 50;
	private int smithoreValue = 75;
	private int crystiteValue = 100;
	
	/**
	 * Creates the panel which contains all the components
	 * 
	 * @param w A reference to the store's wallet
	 * @param p A reference to the Player that is interacting with this store
	 */
	public StorePanel(Wallet w, Player p){
		
		//call to JPanel's constructor
		super();
		
		//save the wallet and the player
		this.w = w;
		this.p = p;
		
		setBorder(new EmptyBorder(5, 5, 5, 5));
		
		//set the layout to a grid with 4 rows, 2 columns, and 5 pixel spacing
		setLayout(new GridLayout(4, 2, 5, 5));
		
		//the label for which resource we're buying selling		
		resource = new JLabel("Resource: ");
		
		//the list of resources to buy/sell
		resourceChooser = new JComboBox(resourceList);
		resourceChooser.setEditable(false);
		resourceChooser.setSelectedItem(0);
		
		//add listener to the resource chooser
		resourceChooser.addActionListener(new ResourceListener());
		
		//create the buy/sell drop downs
		buySelector = new JComboBox();
		sellSelector = new JComboBox();
		
		//buttons to buy/sell a resource		
		buy = new JButton("Buy");
		sell = new JButton("Sell");
		
		//add the listeners to the buttons
		buy.addActionListener(new BuySellListener(true));
		sell.addActionListener(new BuySellListener(false));
		
		market = new JLabel("Market Value: ");
		marketValue = new JLabel(crystiteValue + " each");
		
		//updateSelectors();
		
		//add the components
		add(resource);
		add(resourceChooser);
		add(buySelector);
		add(buy);
		add(sellSelector);
		add(sell);
		add(market);
		add(marketValue);
		
	}//end constructor
	
	/**
	 * Changes the player interacting with this panel
	 * 
	 * @param p The player interacting with this panel
	 */
	public void setPlayer(Player p){
		this.p = p;
	}//end method setPlayer
	
	/**
	 * Updates the buy and sell drop down menu values
	 */
	public void updateSelectors(){
		//remove all the values
		buySelector.removeAllItems();
		sellSelector.removeAllItems();
		//which resource he/she wants to buy or sell
		selectedResource = (String)(resourceChooser.getSelectedItem());
		int storeAmount = 0;
		int playerAmount = 0;
		if(selectedResource.equals("Crystite")){
			storeAmount = w.getCrystite();
			playerAmount = p.getCrystite();
			marketValue.setText(crystiteValue + " each");
		}//end if
		else if(selectedResource.equals("Energy")){
			storeAmount = w.getEnergy();
			playerAmount = p.getEnergy();
			marketValue.setText(energyValue + " each");
		}//end else if
		else if(selectedResource.equals("Food")){
			storeAmount = w.getFood();
			playerAmount = p.getFood();
			marketValue.setText(foodValue + " each");
		}//end else if
		else if(selectedResource.equals("Smithore")){
			storeAmount = w.getSmithore();
			playerAmount = p.getSmithore();
			marketValue.setText(smithoreValue + " each");
		}//end else if
		for(int i = 0; i < (storeAmount + 1); i++){
			buySelector.addItem(new Integer(i));
		}//end for
		for(int i = 0; i < (playerAmount + 1); i++){
			sellSelector.addItem(new Integer(i));
		}//end for
	}//end method updateSliders
	
	/**
	 * Listener for the buy/sell buttons
	 *
	 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
	 * @version %I%, %G%
	 */
	private class BuySellListener implements ActionListener{		
		//is this button the "buy" button or the "sell" button?
		boolean isBuying;		
		/**
		 * Creates a new buy/sell button listener
		 * 
		 * @param isBuying Whether or not this is the buy button or
		 * the sell button
		 */
		public BuySellListener(boolean isBuying){
			this.isBuying = isBuying;
		}//end constructor		
		@Override
		public void actionPerformed(ActionEvent e) {
			//which resource he/she wants to buy or sell
			selectedResource = (String)(resourceChooser.getSelectedItem());
			//the player wants to buy from the store
			if(isBuying){
				//total cost of this transaction
				cost = 0;
				amount = (Integer)(buySelector.getSelectedItem());
				//player wants to buy some crystite
				if(selectedResource.equals("Crystite")){
					//calculate the total cost
					cost = crystiteValue * amount;
					System.out.print("Attempting to buy [" + amount + "] units of ");
					System.out.print("crystite at [" + crystiteValue + "] each for a total cost of [");
					System.out.print(cost + "]. ");
					//has enough money?
					if(p.getMoney() >= cost){
						//deduct money, add crystite
						p.addMoney(-1 * cost);
						p.addCrystite(amount);
						w.addCrystite(-1 * amount);
						System.out.print("Transaction complete.  New balance: [" + p.getMoney() + "] money, ");
						System.out.println("[" + p.getCrystite() + "] crystite.");
					}//end if
					else{
						System.out.println("Transaction cancelled; insufficient funds.");
					}//end else
				}//end if
				//player wants to buy energy
				else if(selectedResource.equals("Energy")){
					//calculate the total cost
					cost = energyValue * amount;
					System.out.print("Attempting to buy [" + amount + "] units of ");
					System.out.print("energy at [" + energyValue + "] each for a total cost of [");
					System.out.print(cost + "]. ");
					//has enough money?
					if(p.getMoney() >= cost){
						//deduct funds, add energy
						p.addMoney(-1 * cost);
						p.addEnergy(amount);
						w.addEnergy(-1 * amount);
						System.out.print("Transaction complete.  New balance: [" + p.getMoney() + "] money, ");
						System.out.println("[" + p.getEnergy() + "] energy.");
					}//end if
					else{
						System.out.println("Transaction cancelled; insufficient funds.");
					}//end else
				}//end else if
				//player wants to buy food
				else if(selectedResource.equals("Food")){
					//calculate cost
					cost = foodValue * amount;
					System.out.print("Attempting to buy [" + amount + "] units of ");
					System.out.print("food at [" + foodValue + "] each for a total cost of [");
					System.out.print(cost + "]. ");
					//has enough money?
					if(p.getMoney() >= cost){
						//deduct money, add food
						p.addMoney(-1 * cost);
						p.addFood(amount);
						w.addFood(-1 * amount);
						System.out.print("Transaction complete.  New balance: [" + p.getMoney() + "] money, ");
						System.out.println("[" + p.getFood() + "] food.");
					}//end if
					else{
						System.out.println("Transaction cancelled; insufficient funds.");
					}//end else
				}//end else if
				//wants to buy smithore
				else if(selectedResource.equals("Smithore")){
					//calculate total cost
					cost = smithoreValue * amount;
					System.out.print("Attempting to buy [" + amount + "] units of ");
					System.out.print("smithore at [" + smithoreValue + "] each for a total cost of [");
					System.out.print(cost + "]. ");
					//has enough funds?
					if(p.getMoney() >= cost){
						//deduct cost, add smithore
						p.addMoney(-1 * cost);
						p.addSmithore(amount);
						w.addSmithore(-1 * amount);
						System.out.print("Transaction complete.  New balance: [" + p.getMoney() + "] money, ");
						System.out.println("[" + p.getSmithore() + "] smithore.");
					}//end if
					else{
						System.out.println("Transaction cancelled; insufficient funds.");
					}//end else
				}//end else if
			}//end if
			//player wants to sell some goods to the store
			else{
				//get the amount of the resource the player wants to sell.  we don't have to
				//  do any error checking in the code below, because the drop downs are 
				//  populated based on the values of each resource the player has.
				amount = (Integer)(sellSelector.getSelectedItem());
				System.out.print("Sold [" + amount + "] units of ");
				//player wants to sell some crystite
				if(selectedResource.equals("Crystite")){
					//add funds, deduct crystite
					p.addMoney(amount * crystiteValue);
					p.addCrystite(-1 * amount);
					w.addCrystite(amount);
					System.out.print("crystite for [" + crystiteValue + "] each. ");
					System.out.print("New balance: [" + p.getCrystite() + "] crystite, ");
				}//end if
				//player wants to sell some energy
				else if(selectedResource.equals("Energy")){
					//add funds, deduct energy
					p.addMoney(amount * energyValue);
					p.addEnergy(-1 * amount);
					w.addEnergy(amount);
					System.out.print("energy for [" + energyValue + "] each. ");
					System.out.print("New balance: [" + p.getEnergy() + "] energy, ");
				}//end else if
				//player wants to sell some food
				else if(selectedResource.equals("Food")){
					//add funds, deduct food
					p.addMoney(amount * foodValue);
					p.addFood(-1 * amount);
					w.addFood(amount);
					System.out.print("foodfor [" + foodValue + "] each. ");
					System.out.print("New balance: [" + p.getFood() + "] food, ");
				}//end else if
				//player wants to sell some smithore
				else if(selectedResource.equals("Smithore")){
					//add funds, deduct smithore
					p.addMoney(amount * smithoreValue);
					p.addSmithore(-1 * amount);
					w.addSmithore(amount);
					System.out.print("smithore for [" + smithoreValue + "] each. ");
					System.out.print("New balance: [" + p.getSmithore() + "] smithore, ");
				}//end else if
				System.out.println("[" + p.getMoney() + "] money.");
			}//end else			
			updateSelectors();			
		}//end method actionPerformed		
	}//end inner class
	
	/**
	 * Listener for the resource dropdown to be changed
	 *
	 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
	 * @version %I%, %G%
	 */
	private class ResourceListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			updateSelectors();
		}//end method actionPerformed		
	}//end inner class
	
}//end class StorePanel
